<?php
/**
 * 功能: 文章管理
 * User: WD
 * Date: 2019/1/4 0004
 * Time: 16:32
 */

namespace app\index\controller;

use think\Controller;
use think\Db;

class Reveal extends Controller{
    /**
     * 文章板块分类
     */
    public function type(){
        $type = Db::name('reveal_type')->select();
        if($type){
            return json(['msg'=>'请求成功','ok'=>true,'data'=>$type]);
        }else{
            return json(['msg'=>'暂无数据','ok'=>false]);
        }

    }

    /**
     * 文章列表
     */
    public function index(){

        $reveal = Db::name('reveal')->field('id,title,images,intro')->paginate(20);

        if($reveal){
            $res = [];
            foreach($reveal as $k=>$v){
                $res[$k] = $v;
                $res[$k]['images'] = array_filter(explode(',',$v['images']));
            }
            return json(['msg'=>'请求成功','ok'=>true,'data'=>$res]);
        }else{
            return json(['msg'=>'暂无数据','ok'=>false]);
        }

    }
    /**
     * 文章详情
     */
    public function detail(){
        $id  =  intval(input('id'));
        $res =  Db::name('reveal')->where('id',$id)->find();
       /* dump($res);
        die;*/
        if($res){
            $res['images'] = array_filter(explode(',',$res['images']));

            return json(['msg'=>'请求成功','ok'=>true,'data'=>$res]);
        }else{
            return json(['msg'=>'暂无数据','ok'=>false]);
        }
    }
    /**
     * 不同分类板块文章
     */
    public function class_reveal(){
        $type = intval(input('id'));//分类id
        $reveal =  Db::name('reveal')
            ->where('type',$type)
            ->field('id,title,images,intro')
            ->paginate(20);

        if($reveal){
            $res = [];
            foreach($reveal as $k=>$v){
                $res[$k] = $v;
                $res[$k]['images'] = array_filter(explode(',',$v['images']));
            }
            return json(['msg'=>'请求成功','ok'=>true,'data'=>$res]);
        }else{
            return json(['msg'=>'暂无数据','ok'=>false]);
        }
    }

    /**
     * 文章搜索
     */
    public function search(){
        $keyword  =  trim(input('keyword'));
        $reveal =  Db::name('reveal')
            ->where('title','like','%'.$keyword.'%')
            ->whereOr('intro','like','%'.$keyword.'%')
            ->whereOr('content','like','%'.$keyword.'%')
            ->field('id,title,images,intro')
            ->paginate(20);

        if($reveal){
            $res = [];
            foreach($reveal as $k=>$v){
                $res[$k] = $v;
                $res[$k]['images'] = array_filter(explode(',',$v['images']));
            }
            return json(['msg'=>'请求成功','ok'=>true,'data'=>$res]);
        }else{
            return json(['msg'=>'暂无数据','ok'=>false]);
        }
    }

}